<?php

class RSSTranslator implements iRSSTranslator {
	
	private
		$aSpecObjects = array ();
	
	public function __construct () {}
	
	/**
	 * @return string
	 */
	public function render (ModuleResponse $oResponse) {
		header ('Content-type: text/xml; charset=utf-8');
		
		$xml = new DOMDocument ('1.0', 'utf-8');
		
		$rssNode = $xml->createElement ('rss');
		$rssNode->setAttribute ('version', '2.0');
		$channelNode = $xml->createElement ('channel');
		
		$channelNode->appendChild ($xml->createElement ('title', CMSController::getPageTitle ()));
		$channelNode->appendChild ($xml->createElement ('language', LANG));
		$channelNode->appendChild ($xml->createElement ('description', ''));
		$channelNode->appendChild ($xml->createElement ('link', CMSController::getLink ()));
		
		$aObjects = $oResponse->getObjects ();
		
		foreach ($aObjects as $object) {
			if ($object instanceof Object && $oElement = $this->getObject ($xml, $object)) {
				$channelNode->appendChild ($oElement);
			}
		}
		
		$rssNode->appendChild ($channelNode);
		$xml->appendChild ($rssNode);
		
		return $xml->saveXML ();		
	}
	
	/**
	 * @param DOMDocument $xml
	 * @param Object $object
	 * @return DOMElement
	 */
	private function getObject (DOMDocument $xml, Object $object) { 		
		$typeId = $object->getType()->getId ();
		
		if (!$oSpec = $this->getSpecObject ($typeId)) {
			return false;
		}
		
		$fieldsController = FieldsController::getInstance ();
		
		$itemNode = $xml->createElement ('item');
		
		if (!$oFieldTitle = $fieldsController->getField ($oSpec->getValue ('title_field'))) {
			return false;
		}
		$itemNode->appendChild ($xml->createElement ('title', $object->getValue ($oFieldTitle->getName ())));
		
		if (!$oFieldDesc = $fieldsController->getField ($oSpec->getValue ('description_field'))) {
			return false;
		}
		
		$description = Jevix::parse ($object->getValue ($oFieldDesc->getName ()));
		
		if ($oFieldImage = $fieldsController->getField ($oSpec->getValue ('image_field'))) {
			$imageURI = $object->getValue ($oFieldImage->getName ());
			
			if ($oSpec->getValue ('image_thumb_size') != '') {
				$thumbSize = $oSpec->getValue ('image_thumb_size');
				$imageURI = 'thumb_' . $thumbSize . '_' . $thumbSize . $imageURI;
			}
			
			$description = '<img src="' . HTTP_HOST . '/' . $imageURI . '" /><br />' . $description;
		}
		
		$description = str_replace ('&', '&amp;', $description);
		$itemNode->appendChild ($xml->createElement ('description', $description));
		
		$linkForemat = $oSpec->getValue ('link_format');
		$objectLink = $this->getObjectLink ($object, $linkForemat);
		$itemNode->appendChild ($xml->createElement ('link', $objectLink));
		
		$guidNode = $xml->createElement ('guid', $objectLink);
		$guidNode->setAttribute ('isPermaLink', 'true');
		$itemNode->appendChild ($guidNode);
		
		if ($oFieldOwner = $fieldsController->getField ($oSpec->getValue ('owner_name_field'))) {
			$itemNode->appendChild ($xml->createElement ('author', $object->getValue ($oFieldOwner->getName ())));
		}
		
		if ($oFieldDate = $fieldsController->getField ($oSpec->getValue ('pubdate_field'))) {
			$gmtDate = gmdate('D, d M Y H:i:s', $object->getValue ($oFieldDate->getName ())) . ' GMT';
			$itemNode->appendChild ($xml->createElement ('pubDate', $gmtDate));
		}
		
		if ($oFieldTags = $fieldsController->getField ($oSpec->getValue ('tags_field'))) {
			if ($oFieldTags->getCharacter () == 'tags') {
				$tags = $object->getValue ($oFieldTags->getName ());
				if (is_array ($tags)) {
					foreach ($tags as $tag) {
						$tag = str_replace ('&', '&amp;', $tag);
						$itemNode->appendChild ($xml->createElement ('category', $tag));
					}
				}
			}
		}
		
		return $itemNode;
	}
	
	private function getSpecObject ($typeId) {
		if (!empty ($this->aSpecObjects[$typeId])) {
			return $this->aSpecObjects[$typeId];
		}
		
		$oTypeRss = TypesController::getInstance()->getTypeByName ('rss_type');
		
		$sel = new Selection;
		$sel->addType ($oTypeRss);
		$sel->addFilter ('type_id', $typeId);
		$sel->addLimit (1);
		
		$res = $sel->getList ();
		
		if (!count ($res)) {
			return $this->aSpecObjects[$typeId] = false;
		}
		
		return $this->aSpecObjects[$typeId] = ObjectsController::getInstance()->getObject ($oTypeRss, $res[0][0]);
	}
	
	public function getObjectLink (Object $object, $link) {
		$arr1 = array ('%id%','%type-name%','%type-id%');
		$arr2 = array ($object->getId(), $object->getTypeName(), $object->getType()->getId());
		
		$link = str_replace ($arr1, $arr2, $link);
		
		return HTTP_HOST . '/' . $link;
	}
	
}